<?php

class UserController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	//$user = new Application_Model_UserMapper();
    	//$this->view->entries = $user->fetchAll();
        // action body
        
    	$auth = Zend_Auth::getInstance();
        if($auth->hasIdentity()){
        	//if loged in
        	
        	$this->view->note = "loged in";
        }
        else{
        	//not loged in
        	$form    = new Application_Form_Login();
        	//$this->view->form = $form;
        	$this->view->note = "not loged in";
        	$this->_helper->redirector('login');
        }
    	
    }

    public function loginAction()
    {
        // action body
    	$request = $this->getRequest();
    	$form    = new Application_Form_Login();
    	
    	$auth = Zend_Auth::getInstance();
    	
    	if($auth->hasIdentity()){
    		//if loged in
    		 
    		$this->view->note = "loged in";
    		//redirect to index
    		$this->_helper->redirector('index');
    	}
    	else{
    		//not loged in, do login

    		
    		if ($this->getRequest()->isPost()) {
    			if ($form->isValid($request->getPost())) {
    				Zend_Loader::loadClass('Zend_Filter_StripTags');
    				$filter=new Zend_Filter_StripTags();
    				$username = $filter->filter($this->_request->getPost('mobile_tel'));
    				$password = $filter->filter($this->_request->getPost('password'));
    				//$validcode = $filter->filter($this->_request->getPost('validcode'));//验证码
    				if(!empty($username)){
    		
    		
    		
    					$mapper = new Application_Model_UserMapper();
    					$result = $mapper->validateMobileLogin($username,$password);
			
    					if($result->isValid()){
    						if($auth->hasIdentity()){
    							//login valid
    							$userSession = new Zend_Session_Namespace('user');
    							$userSession->username = $username;
    							//$userSession->
    							$userSession->setExpirationSeconds(3600);//session expires in one hour
    							//get user infomation
    							//$auth = Zend_Auth::getInstance();
    							//$userinfo = $auth->getIdentity();
    							$this->_redirect('dashboard/index');// jump to index action
    							//$this->view->entries = ;
    							
    						}else{
    							$this->view->note= "login failed no identity";
    						}

    					}else{
    						//login failed
    						$this->view->note= "login failed username=".$username." password=".$password;
    					}
    				}else{
    					//username empty
    					$this->view->note= "user name empty";
    				}
    			}else{
    				//form data not valid
    				$this->view->note= "form data not vaild";
    			}
    		}else{
    			//not post request
    			//illegal
    			//$this->view->note= "illegal action";
    		}
    		$this->view->form = $form;
    	}	
    }

    public function logoutAction()
    {
        // action body
    	$user = new Zend_Session_Namespace('user');
    	$user->__unset();//销毁session
    	Zend_Auth::getInstance()->clearIdentity();
    	$this->_helper->redirector('index');
    	echo '已经安全退出!';

    }

    public function registerAction()
    {
        // action body
        
    }

    public function dumpAction()
    {
        // action body
    	$user = new Application_Model_UserMapper();
    	$this->view->entries = $user->fetchAll();
    }

    public function dashboardAction()
    {
        // action body
    	$userSession = new Zend_Session_Namespace('user');
    }

    public function addressbookAction()
    {
        // action body
    	$userSession = new Zend_Session_Namespace('user');
    	
    }


}













